package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.domain.TableVO;
import com.zerox.pojo.Supplier;
import com.zerox.service.SysSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;


@Controller
public class GongYingController {

    @Autowired
    private SysSupplierService sysSupplierService;
    @ResponseBody
    @GetMapping(value = "/list",produces = {"application/json;charset=utf-8"})
    public void list(Model model, HttpServletResponse response,Integer page, Integer limit){
        Integer count = sysSupplierService.count();

        List<Supplier> supplierList = sysSupplierService.list(page,limit);
        TableVO tableVO = new TableVO(0,count,supplierList,"");

        String json = JSON.toJSONString(tableVO);
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().print(json);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    @GetMapping("/GongYing")
    public String getList(){
        return "GongYing";
    }

    @GetMapping("/toaddSupplier")
    public String toAddSupplier(){
        return "AddSupplier";
    }


    @PostMapping("/AddSupplierTo")
    public String AddSupplier(Model model, @Valid Supplier supplier, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors){
                model.addAttribute(fieldError.getField()+"Error",fieldError.getDefaultMessage());
            }
            return "AddSupplier";
        }

        int result = sysSupplierService.add(supplier);
        if (result>0){
            return "redirect:GongYing";
        }
        model.addAttribute("errorMsg","新增失败");
        return "forward:AddSupplier/"+supplier.getId();
    }

    @GetMapping("/viewSupplier/{id}")
    public String viewSupplier(Model model, @PathVariable Integer id){
        Supplier suppLier = sysSupplierService.selectById(id);
        model.addAttribute("suppLier",suppLier);
        return "viewSupplier";
    }

    @GetMapping("/supplierDelete/{id}")
    @ResponseBody
    public String delete(@PathVariable Integer id){

        int result = sysSupplierService.delete(id);
        return result>0 ? "1" : "0";
    }

    @GetMapping("/toSupplierUpdate/{id}")
    public String SupplierUpdate(Model model,@PathVariable Integer id){
        Supplier suppLier = sysSupplierService.selectById(id);
        model.addAttribute("supplier",suppLier);
        return "SupplierUpdate";
    }

    @PostMapping("/toSupplierUpdate")
    public String SupplierUpdate(Model model,@Valid Supplier supplier,BindingResult bindingResult){
    if (bindingResult.hasErrors()){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors){
            model.addAttribute(fieldError.getField()+"Error",fieldError.getDefaultMessage());
        }
        return "SupplierUpdate";
    }
        int result = sysSupplierService.update(supplier);
        if (result>0){
            return "redirect:GongYing";

        }
        model.addAttribute("errorMsg","修改失败");
        return "SupplierUpdate";
    }

    @GetMapping("/supCodeExists")
    @ResponseBody
    public String supCodeExists(String supCode){
        Supplier supplier = sysSupplierService.selectBysupCode(supCode);
        return supplier==null?"true":"false";
    }

    @GetMapping(value = "/view2{id}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String view2(@PathVariable Integer id,Model model){
        Supplier supplier = sysSupplierService.selectId(id);
        String json = JSON.toJSONString(supplier);
        return json;
    }
}
